// Ray Bai
// Creating a Telephone Directory in C++
// 5/31/2011

#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <stdlib.h>

#define BUFSIZE 100

using namespace std;

int main()
{
  char name[BUFSIZE];
  char telnum[BUFSIZE];
  int selection;
    
  ofstream outfile("directory.txt");
  if(!outfile) {
    cerr << "Output file could not be created" << endl;
    exit(1);
  }
   
  do {
    // Display the menu and allow user to choose 1 or 2.
    cout << endl << endl;
    cout << "\t1. Update telephone directory.\n";
    cout << "\t2. Quit program. \n\n";
    cout << "\tEnter your choice (1-2): ";
    cin >> selection;
    cin.clear(); cin.ignore(1024, '\n');
        
    // Validate menu selection.
    if (selection < 1 || selection > 2)
      {
        cout << endl;
        cout << "\n\n\t**Error: Menu option must be 1 or 2.\n\n";
      }
    else if (selection == 1)
      {
        cout << "\t Enter name: ";            
        cin.getline(name, BUFSIZE);

        cout << endl << "\n\t Enter telephone #: ";
        cin.getline(telnum, BUFSIZE);

        outfile << setw(30) << right << name << "\t";
        outfile << setw(13) << right << telnum << endl;
        cout << "\n\n\t Record stored. \n\n";
      }
    else if (selection == 2) // Quit program
      {         
        cout << "\n\n You have now exited the program. \n\n";
      }
  } while(selection != 2);

  system("pause");
  return 0;
}
